package com.smart.campus.web.controller.school;

import java.util.List;

import com.mchange.v2.cmdline.CommandLineUtils;
import com.smart.campus.school.domain.ExamRoomClasses;
import com.smart.campus.school.domain.ExamRoomSchedule;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.service.IExamRoomClassesService;
import com.smart.campus.school.service.IExamRoomScheduleService;
import com.smart.campus.school.service.IStudentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.ExamRoomStudents;
import com.smart.campus.school.service.IExamRoomStudentsService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 考场学生安排Controller
 * 
 * @author Zhihe.Li
 * @date 2020-06-06
 */
@Controller
@RequestMapping("/school/roomStudents")
public class ExamRoomStudentsController extends BaseController
{
    private String prefix = "school/roomStudents";

    @Autowired
    private IExamRoomStudentsService examRoomStudentsService;

    @Autowired
    private IExamRoomClassesService examRoomClassesService;

    @RequiresPermissions("school:roomStudents:view")
    @GetMapping()
    public String roomStudents()
    {
        return prefix + "/roomStudents";
    }

    @RequiresPermissions("school:roomStudents:list2")
    @GetMapping("/getStudentsList/{clazzId}")
    public String getStudentsList(@PathVariable Long clazzId,ModelMap mmap)
    {
        ExamRoomClasses room = examRoomClassesService.selectExamRoomClassesById(clazzId);
        mmap.put("room",room);
        return prefix + "/roomStudents2";
    }

    /**
     * 查询考场学生安排列表
     */
    @RequiresPermissions("school:roomStudents:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamRoomStudents examRoomStudents)
    {
        startPage();
        List<ExamRoomStudents> list = examRoomStudentsService.selectExamRoomStudentsList(examRoomStudents);
        return getDataTable(list);
    }

    /**
     * 生成考场学生记录
     */
    @RequiresPermissions("school:roomStudents:distributionRoomByNO")
    @GetMapping("/distributionRoomByNO/{scheduleId}")
    @Log(title = "考场学生安排", businessType = BusinessType.INSERT)
    @ResponseBody
    public AjaxResult distributionRoomByNO(@PathVariable("scheduleId") Long scheduleId)
    {
        int count = examRoomStudentsService.checkScheduleStudents(scheduleId);
        if(count>0){
            return AjaxResult.warn("已经生成过考场安排！");
        }
        return examRoomStudentsService.distributionRoomByNO(scheduleId);
    }

    /**
     * 导出考场学生安排列表
     */
    @RequiresPermissions("school:roomStudents:export")
    @Log(title = "考场学生安排", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamRoomStudents examRoomStudents)
    {
        List<ExamRoomStudents> list = examRoomStudentsService.selectExamRoomStudentsList(examRoomStudents);
        ExcelUtil<ExamRoomStudents> util = new ExcelUtil<ExamRoomStudents>(ExamRoomStudents.class);
        return util.exportExcel(list, "roomStudents");
    }

    /**
     * 新增考场学生安排
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存考场学生安排
     */
    @RequiresPermissions("school:roomStudents:add")
    @Log(title = "考场学生安排", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamRoomStudents examRoomStudents)
    {
        return toAjax(examRoomStudentsService.insertExamRoomStudents(examRoomStudents));
    }

    /**
     * 修改考场学生安排
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamRoomStudents examRoomStudents = examRoomStudentsService.selectExamRoomStudentsById(id);
        mmap.put("examRoomStudents", examRoomStudents);
        return prefix + "/edit";
    }

    /**
     * 修改保存考场学生安排
     */
    @RequiresPermissions("school:roomStudents:edit")
    @Log(title = "考场学生安排", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamRoomStudents examRoomStudents)
    {
        return toAjax(examRoomStudentsService.updateExamRoomStudents(examRoomStudents));
    }

    /**
     * 删除考场学生安排
     */
    @RequiresPermissions("school:roomStudents:remove")
    @Log(title = "考场学生安排", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examRoomStudentsService.deleteExamRoomStudentsByIds(ids));
    }
}
